const app = getApp();
import {
  post
} from '../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginInfo: null,
    config: null,
    statusBarHeight: app.globalData.statusBarHeight,
    titleHeight: app.globalData.titleHeight,
    type: null,
    detail: null,
  },

  onBack() {
    wx.navigateBack();
  },

  // 保存海报
  onSavePoster() {
    let options = app.getPageOptions();
    // this.loadFontFace();
    this.createSelectorQuery().select("#myArea")
      .node().exec(res => {
        console.log(res);
        const node = res[0].node
        node.takeSnapshot({
          type: 'arraybuffer',
          format: 'png',
          success: (res) => {
            const f = `${wx.env.USER_DATA_PATH}/${options.cid}.png`
            const fs = wx.getFileSystemManager();
            fs.writeFileSync(f, res.data, 'binary')
            wx.showToast({
              title: '保存成功'
            })

            wx.saveImageToPhotosAlbum({
              filePath: f,
              complete(res) {
                console.log("saveImageToPhotosAlbum:", res)
              }
            })
          },
          fail(res) {
            console.log("takeSnapshot fail:", res)
          }
        })
      });
  },

  // 获取二维码
  getQrcode(id) {
    let loginInfo = app.getLoginInfo();
    post(`/qrcode/unlimit`, {
        width: 90,
        page: 'pages/campain/index',
        scene: `${id}-${loginInfo.code}`
      })
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            qrcode: resp.data.url
          });
        } else {
          wx.showModal({
            title: '提示',
            content: resp.msg,
            showCancel: false,
            success() {

            }
          })
        }
      });
  },

  // 获取商品
  getGoods(id) {
    post(`/mall/goods/detail/${app.globalData.tenantCode}/${id}`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            detail: resp.data,
          });
        } else {
          wx.showModal({
            title: '提示',
            content: resp.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      });
  },

  // 获取服务
  getServe(id) {
    post(`/mall/serve/detail/${app.globalData.tenantCode}/${id}`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            detail: resp.data
          });
        } else {
          wx.showModal({
            title: '提示',
            content: resp.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      });
  },

  // 获取套餐
  getPkg(id) {
    post(`/mall/pkg/detail/${app.globalData.tenantCode}/${id}`)
      .then(resp => {
        if (resp.code === 0) {
          this.setData({
            detail: resp.data
          });
        } else {
          wx.showModal({
            title: '提示',
            content: resp.msg,
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let options = app.getPageOptions();
    // 获取二维码
    this.getQrcode(options.cid);
    // 商品
    if (options.type === '0') {
      this.getGoods(options.aid);
    }
    // 服务
    if (options.type === '1') {
      this.getServe(options.aid);
    }
    // 套餐
    if (options.type === '2') {
      this.getPkg(options.aid);
    }

    app.getConfig(config => {
      this.setData({
        config
      });
    });

    this.setData({
      loginInfo: app.getLoginInfo(),
      type: options.type,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let imageUrl;
    if (this.data.type=='0' || this.data.type=='1') {
      imageUrl = this.data.detail.rawAlbum[0];
    }
    if (this.data.type=='2') {
      imageUrl = this.data.detail.img;
    }

    let options = app.getPageOptions();

    return {
      title: this.data.detail.name,
      path: `/pages/campain/index?scene=${options.cid}-${this.data.loginInfo.code}`,
      imageUrl: imageUrl
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareTimeline() {
    let imageUrl;
    if (this.data.type=='0' || this.data.type=='1') {
      imageUrl = this.data.detail.rawAlbum[0];
    }
    if (this.data.type=='2') {
      imageUrl = this.data.detail.img;
    }

    let options = app.getPageOptions();

    return {
      title: this.data.detail.name,
      path: `/pages/campain/index?scene=${options.cid}-${this.data.loginInfo.code}`,
      imageUrl: imageUrl
    }
  },

});